#include <iostream>
#include <iomanip>
#include <memory.h>
#include <sstream>
#include <assert.h>

#include "HashValue.hpp"

HashValue::HashValue(const unsigned int length) : length(length) {
	assert(length <= 16);
}

HashValue::HashValue(const unsigned char data[], const unsigned int length) : length(length) {
	assert(length <= 16);
	set(data);
}

std::string HashValue::toHex(bool separated) const {
	std::ostringstream os; 
	for (unsigned int i = 0; i < length; i++) {
		if (separated && (i != 0)) os << ":";
		os << std::hex << std::setfill('0') << std::setw(2) << (int)data[i];
	}
	return os.str();
}

void HashValue::set(const unsigned char *data) {
	memcpy(this->data, data, length);
}

unsigned char* HashValue::getDataPtr() {
	return data;
}

HashValue::~HashValue() {
}

bool HashValue::operator<(const HashValue &other) const {
	for (unsigned int i = 0; i < length; i++) {
		if (other.data[i] > data[i]) return true;
			else if (other.data[i] < data[i]) return false;
	}
	return false;
}

bool HashValue::operator==(const HashValue &other) const {
	for (unsigned int i = 0; i < length; i++) {
		if (other.data[i] != data[i]) return false;
	}
	return true;
}


